import pandas as pd
import logging
from models.configuration_model import FormAll
from config import SETTINGS

def load_form_all_data():
    try:
        excel_file = pd.ExcelFile(SETTINGS['DEFAULT_STRUCTURE_SPEC'])
        df = excel_file.parse('FormAll')
        form_all_objs = []
        for _, row in df.iterrows():
            try:
                form_all_obj = FormAll(
                    FolderOID=str(row['FolderOID']),
                    FolderName=str(row['FolderName']),
                    ModuleOID=str(row['ModuleOID']),
                    FolderModuleName=str(row['FolderModuleName']),
                    IsICF=bool(row['IsICF']),
                    IsDemographics=bool(row['IsDemographics']),
                    IsDrug=bool(row['IsDrug']),
                    IsReview=bool(row['IsReview']),
                    IsEnrollment=bool(row['IsEnrollment']),
                    IsSummary=bool(row['IsSummary']),
                    IsLab=bool(row['IsLab']),
                    IsBaseline=bool(row['IsBaseline']),
                    IsOutGroup=bool(row['IsOutGroup']),
                    IsFilter=bool(row['IsFilter']),
                    IsCheckIn=bool(row['IsCheckIn']),
                    IsTrial=bool(row['IsTrial']),
                    IsVisitCycle=bool(row['IsVisitCycle']),
                    IsBloodDraw=bool(row['IsBloodDraw']),
                    IsLbup=bool(row.get('IsLbup', False)),
                )
                form_all_objs.append(form_all_obj)
            except KeyError as e:
                logging.error(f"FormAll表中不存在列: {str(e)}")
                raise
        return form_all_objs
    except Exception as e:
        logging.error(f"加载FormAll数据时发生错误: {str(e)}")
        raise